<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="光开关ID" prop="switchId">
      <el-input v-model="dataForm.switchId" placeholder="光开关ID"></el-input>
    </el-form-item>
    <el-form-item label="FBG ID" prop="fbgId">
      <el-input v-model="dataForm.fbgId" placeholder="FBG ID"></el-input>
    </el-form-item>
    <el-form-item label="深度" prop="deep">
      <el-input v-model="dataForm.deep" placeholder="深度"></el-input>
    </el-form-item>
    <el-form-item label="初始波长" prop="initWavelength">
      <el-input v-model="dataForm.initWavelength" placeholder="初始波长"></el-input>
    </el-form-item>
    <el-form-item label="初始波长差" prop="initWavelengthDiff">
      <el-input v-model="dataForm.initWavelengthDiff" placeholder="初始波长差"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="gmtModify">
      <el-input v-model="dataForm.gmtModify" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          switchId: '',
          fbgId: '',
          deep: '',
          initWavelength: '',
          initWavelengthDiff: '',
          gmtCreate: '',
          gmtModify: ''
        },
        dataRule: {
          switchId: [
            { required: true, message: '光开关ID不能为空', trigger: 'blur' }
          ],
          fbgId: [
            { required: true, message: 'FBG ID不能为空', trigger: 'blur' }
          ],
          deep: [
            { required: true, message: '深度不能为空', trigger: 'blur' }
          ],
          initWavelength: [
            { required: true, message: '初始波长不能为空', trigger: 'blur' }
          ],
          initWavelengthDiff: [
            { required: true, message: '初始波长差不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          gmtModify: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/sensor/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.switchId = data.sensor.switchId
                this.dataForm.fbgId = data.sensor.fbgId
                this.dataForm.deep = data.sensor.deep
                this.dataForm.initWavelength = data.sensor.initWavelength
                this.dataForm.initWavelengthDiff = data.sensor.initWavelengthDiff
                this.dataForm.gmtCreate = data.sensor.gmtCreate
                this.dataForm.gmtModify = data.sensor.gmtModify
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/sensor/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'switchId': this.dataForm.switchId,
                'fbgId': this.dataForm.fbgId,
                'deep': this.dataForm.deep,
                'initWavelength': this.dataForm.initWavelength,
                'initWavelengthDiff': this.dataForm.initWavelengthDiff,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModify': this.dataForm.gmtModify
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
